package com.powernode.config;

import com.powernode.constant.AuthConstants;
import com.powernode.constant.HttpConstants;
import com.powernode.impl.UserDetailsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.annotation.Resource;
import java.util.UUID;


/**
 * security配置类
 */

@Configuration
@Slf4j
public class AuthSecurityConfig extends WebSecurityConfigurerAdapter {



    /**
     * 依赖注入容器中实现UserDetailsServic接口的实现类
     */
    @Resource
    private UserDetailsServiceImpl userDetailsService;



    /**
     * 认证模块
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
       //关闭跨域伪造
        http.cors().disable();
        //关闭跨域伪造
        http.csrf().disable();
        //关闭session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        //配置登录信息
        http.formLogin()
                .loginPage(AuthConstants.LOGIN_URL) //设置登录url
                .successHandler(authSuccessHandler()) //登录成功返回
                .failureHandler(authFailureHandler()); //登录失败返回
         //配置退出信息
        http.logout()
                .logoutUrl(AuthConstants.LOGOUT_URL)
                .logoutSuccessHandler(logoutSuccessHandler());
         //要求所有请求都认证
        http.authorizeRequests().anyRequest().authenticated();
    }

    /**
     * security走自己的安全认证流程
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    /**
     * 登录成功处理器
     * @return
     */
    @Bean
    public AuthenticationSuccessHandler authSuccessHandler() {
        return (request, response, authentication) -> {
            //设置相应头
            response.setContentType(HttpConstants.CONTENT_TYPE);
            response.setCharacterEncoding(HttpConstants.UTF_8);
            //使用uuid当做token
            String token = UUID.randomUUID().toString();
            //取出认证的对象


        };
    }

    /**
     * 登录失败处理器
     * @return
     */
    @Bean
    public AuthenticationFailureHandler authFailureHandler() {
        return (request, response, authentication) -> {

        };
    }

    /**
     * 退出成功处理器
     * @return
     */
    @Bean
    public LogoutSuccessHandler logoutSuccessHandler(){
        return (request, response, authentication) -> {

        };
    }

    /**
     * 密码加密器
     * @return
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
